package ru.romannep.pc.chopperbattle;

import java.net.Socket;
import java.util.ArrayList;

class ChopperBattlePlayer extends Connection {

    String name;
    PlayerState state;

//    void processMessageWhileConnecting(NetworkMessage msg) {
//
//        switch (msg.type) {
//            case SET_NAME:
//                name = msg.s;
//                state = PlayerState.CHATTING;
//                ChopperBattleGameServer.getGameServer().listOnlinePlayers(this);
//                break;
//        }
//
//    }
//
//    void processMessageWhileChatting(NetworkMessage msg) {
//        switch (msg.type) {
//            case CHAT_MESSAGE:
//                log("chat msg from " + name + ": " + msg.s);
//                ChopperBattleGameServer.getGameServer().chatMessage(msg.s, this);
//                break;
//        }
//    }

    @Override
    void incomingMessage(NetworkMessage msg) {
        //log("<-" + socket.getInetAddress() + ": " + msg.s);
//        switch (state) {
//            case CONNECTING:
//                processMessageWhileConnecting(msg);
//                break;
//            case CHATTING:
//                processMessageWhileChatting(msg);
//                break;
//        }
        switch (msg.type) {
            case SET_NAME:
                name = msg.s;
                state = PlayerState.CHATTING;
                ChopperBattleGameServer.getGameServer().listOnlinePlayers(this);
                ChopperBattleGameServer.getGameServer().startGame();
                break;
        
            case CHAT_MESSAGE:
                log("chat msg from " + name + ": " + msg.s);
                ChopperBattleGameServer.getGameServer().chatMessage(msg.s, this);
                break;
            case GAME_DATA:
                //отправим другому
                log("data: " + msg.s + " com = " + msg.commands );
                //System.out.println("BP in data "+ msg.s + " com[0]=" + msg.commands.get(0).gameCircle + " com = " + msg.commands);
                ChopperBattleGameServer.getGameServer().sendGameData(msg.commands, this);
                
                break;
        
        
        }

    
    }

    void sendGameData(ArrayList<ChopperCommands> commands){

        //System.out.println("BP out data com[0]="+ commands.get(0).gameCircle + " com = " + commands);
        sendMessage(new NetworkMessage(MessageType.GAME_DATA, "" + commands.get(0).gameCycle, commands));
        
    }
    
    void sendChatMessage(String message) {
        
        sendMessage(new NetworkMessage(MessageType.CHAT_MESSAGE, message));
        
    }
    
    @Override
    void connectError(Exception ex) {
        super.connectError(ex);
        log("Err " + ex);
    }

    @Override
    void onDisconnect(Exception ex) {
        ChopperBattleGameServer.getGameServer().removePlayer(this);
    }

    public ChopperBattlePlayer(Socket s) {
        super(s);
        this.state = PlayerState.CONNECTING;
    }

    private void log(String s) {
        ChopperBattleGameServer.getGameServer().log(s);
    }
}